package com.detrum.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.detrum.utils.LogUtils;

import java.lang.reflect.Field;

/**
 * Created by abc on 2017/7/18.
 */

public class SqliteHelper extends SQLiteOpenHelper {
    private static SqliteHelper helper;
    private SQLiteDatabase database;

    public static SqliteHelper getInstance(Context context){
        if(helper==null){
            helper=new SqliteHelper(context);
        }
        return helper;
    }
    private SqliteHelper(Context context) {
        super(context, "Detrum", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        database = db;
        String tableName1 = "create table IF NOT EXISTS Device_info (name varchar(20),msg TEXT,image int,imagePath TEXT,price double,marketTime TEXT)";
        db.execSQL(tableName1);
        String tableName2 = "create table IF NOT EXISTS fileList (position int,nfsPath TEXT,fileName TEXT,cachePath TEXT, thumbPath TEXT, size int,fileTime long,time String,isNfs int,isLocal int)";
        db.execSQL(tableName2);
        String tableName3 = "create table IF NOT EXISTS Device_Version (name varchar(20),deviceVersion TEXT NOT NULL,version TEXT,length int,verify TEXT,downloadPath TEXT,filePath TEXT,imagePath TEXT,explain TEXT,updateMsg TEXT,updateFilePath TEXT,updateTime long)";
        db.execSQL(tableName3);
        String tableName4 = "create table IF NOT EXISTS Share_Info (name TEXT,packageName TEXT,activityName TEXT,isSelect int)";
        db.execSQL(tableName4);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void CreateTable(Object obj){
        if(obj==null){
            Log.e("SqliteHelper","CreateTable input obj is null SqliteHelper 50");
            return;
        }
        String tableName = obj.getClass().getSimpleName();
        Field[]fields = obj.getClass().getDeclaredFields();
        if(fields==null){
            Log.e("SqliteHelper","CreateTable input obj fields is null SqliteHelper 55");
            return;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < fields.length; i++) {
            String column = fields[i].getName().toLowerCase();
            stringBuffer.append(column+" ");
            String name = fields[i].getType().getSimpleName();
            if(name.equals("String")){
                if(i!=fields.length-1){
                    stringBuffer.append("TEXT,");
                }else{
                    stringBuffer.append("TEXT");
                }
            }else if(name.equals("int")){
                if(i!=fields.length-1){
                    stringBuffer.append("int,");
                }else{
                    stringBuffer.append("int");
                }
            }else if(name.equals("long")){
                if(i!=fields.length-1){
                    stringBuffer.append("long,");
                }else{
                    stringBuffer.append("long");
                }
            }else if(name.equals("double")){
                if(i!=fields.length-1){
                    stringBuffer.append("double,");
                }else{
                    stringBuffer.append("double");
                }
            }else if(name.equals("byte[]")){
                if(i!=fields.length-1){
                    stringBuffer.append("Blob,");
                }else{
                    stringBuffer.append("Blob");
                }
            }else{
                Log.i("SqliteHelper","Create table fieldName = "+name);
                continue;
            }
        }
        if(database==null){
            database = getWritableDatabase();
        }
        database.execSQL("create table IF NOT EXISTS "+tableName+"("+stringBuffer.toString()+")");
    }
}
